import React from 'react';
import {Modal, message} from 'antd';
import axios from './axios';
import QuickTool from './QuickTool';

export default {


  //分页快捷封装
  pagination(paginationData, callback) {
    return {
      defaultPageSize: 10,	                                  //默认的每页条数  number	10	
      defaultCurrent: 1,                                        //默认的当前页数  number	1
      current: paginationData.content.page.page,                //当前页数        number
      pageSize: paginationData.content.page.size,               //每页条数
      total: paginationData.content.page.total,                 //数据总数        number	0	
      showQuickJumper: true,                                    //用于显示数据总量和当前数据顺序	Function(total, range)	-	
      showSizeChanger: true,                                    //是否可以改变 pageSize,可以联合pageSizeOptions使用
      pageSizeOptions: ['10', '20', '50', '100'],	              //指定每页可以显示多少条  string[]	['10', '20', '30', '40']
      size: "small",                                            //当为「small」时，是小尺寸分页	string	""
      hideOnSinglePage: true,                                  //只有一页时是否隐藏分页器	boolean	false	3.1.0
      disabled: false,                                          //禁用分页	boolean	-	3.18.0
      // simple:true,                                           //当添加该属性时，显示为简单分页  boolean
      // itemRender	                                            // TODO 用于自定义页码的结构，可用于优化 SEO	(page, type: 'page' | 'prev' | 'next', originalElement) => React.ReactNode	-	
      // showLessItems	                                        //show less page items	boolean	false	3.16.3
      showTotal: () => {                                        //用于显示数据总量和当前数据顺序	Function(total, range)	-	
        return `共${paginationData.content.page.total}条`;
      },
      onChange: (current, size) => {                            //页码改变的回调，参数是改变后的页码及每页条数 Function(page, pageSize)
        callback(current, size, "onChange");
      },
      onShowSizeChange: (current, size) => {                    //pageSize 变化的回调  Function(current, size)
        callback(current, size, "onShowSizeChange");
      },
      // onShowSizeChange: (current, size) => {                //pageSize 变化的回调  Function(current, size)
      //   let newPage = this.props.capital.page;
      //   newPage.size = size;
      //   newPage.page = 1;
      //   this.props.dispatch({
      //     type: 'capital/getStatementsList',
      //     payload: {
      //       customerno: values.customerno,
      //       stateno: values.stateno,
      //       status: values.status,
      //       beginTime: beginTime,
      //       endTime: endTime,
      //       minamount: values.minamount,
      //       maxamount: values.maxamount,
      //       creator: values.creator,
      //       page: newPage
      //     }
      //   });
      // },

    };
  },


  //搜索面板数据映射
  mapping(params) {
    let obj = {};
    params.searchFieldList
      .filter(item => {
        return item.type == 'BUTTON';
      })
      .map(item => {
        if (!item['callback']) {
          obj[item.field] = function (searchReturnData) {
            params.page.page = 1;
            params.page = {...params.page, ...searchReturnData};
            params.getListForPage();
            params.setState({
              loadingTableisHide: false,
            });
          };
        } else if (!item['callback']['mappingfun']) {
          item['callback'] &&
          (obj[item['callback']['name']] = function (searchReturnData) {
            params.page.page = 1;
            params.page = {...params.page, ...searchReturnData};
            params.getListForPage();
            params.setState({
              loadingTableisHide: false,
            });
          });
        } else {
          item['callback'] && (obj[item['callback']['name']] = item['callback']['mappingfun']);
        }
      });

    obj['search'] = (searchReturnData) => {
      params.page.page = 1;
      params.page = {...params.page, ...searchReturnData};
      params.getListForPage();
      params.setState({
        loadingTableisHide: false,
      });
    };

    obj['reset'] = (searchReturnData) => {
      params.page.page = 1;
      params.page = {...params.page, ...searchReturnData};
      params.getListForPage();
      params.setState({
        loadingTableisHide: false,
      });
    };

    obj['expand'] = params.state.expand;
    obj['searchNum'] = params.searchFieldList.length;
    if (!params.state.expand) {
      obj['searchFieldList'] = params.searchFieldList.slice(0, 2); //todo kim-stamp 搜索面板的搜索条件数
    } else {
      params.searchFieldList = params.searchFieldList;
    }
    obj['collapse'] = (isexpand) => {
      if (isexpand) {
        params.setState({
          expand: true,
          searchFieldList: params.searchFieldList,
        });
        params.searchFieldList = params.state.searchFieldList || params.searchFieldList;
        // console.log(params.searchFieldList);
      } else {
        params.setState({
          expand: false,
        });
        params.searchFieldList = params.state.searchFieldList;
      }
    };


    return obj;
  },

  //查询api 请求页面
  async getListForPage(params, callback) {
    if (params instanceof Object) {
      var transfer = params;
    } else if (typeof params == 'string') {
      var transfer = {url: params};
    }
    let res = await axios.ajax({
      url: transfer.url,
      params: transfer.data || this.page
    });
    if (res && callback) {
      callback(res)
    }
    if (res && res.content && res.content.list) {
      let pageListData = res.content.list.map((item, index) => {
        item.key = index;
        return item;
      });
      this.setState({
        pageListData,
        loadingTableisHide: true,
        pagination: QuickTool.pagination(
          res,
          (current, size, type) => {
            console.log(current, size)
            if (type === "onChange") {
              this.page.page = current;
              this.getListForPage();
            } else {
              this.page.page = 1;
              this.page.size = size;
              this.getListForPage();
            }
          },
        ),
      });
    }
  },
  async handleFinishOrder(params) {
    if (params instanceof Object) {
      var transfer = params;
    } else if (typeof params == 'string') {
      var transfer = {url: params};
    }

    let _this = this;
    let res = await axios.ajax({
      url: transfer.url,
      params: {
        orderId: _this.state.orderId,
      },
    });
    if (res.code == 0) {
      await this.setState({
        defaultModalVisble: false,
        selectedRowKeys: ''
      });
      this.getListForPage();
      message.success('订单结束成功', 1);
      // message.success('订单结束成功', 1, function () { console.log("ApiTubro --- message关闭回调") })
    }
  },

  async handleMultipleDelect(params) {
    if (params instanceof Object) {
      var transfer = params || {};
    } else if (typeof params == 'string') {
      var transfer = {url: params};
    } else {
      var transfer = {};
    }
    this.setState({loading: true});


    Modal.info({
      title: '确定要删除',
      content: this.state.selectedRows.map(item => item['user_name'])
        .join(','),
      onOk: async () => {
        let res = axios.ajax({
          url: transfer.url,
          params: transfer.data
        });
        if (res) {
          await this.getListForPage();
          await this.setState({
            loading: false,
            selectedRows: '',
            selectedRowKeys: '',
          });
          message.success('批量删除成功', 1);
        } else {
          message.success('没有请求url', 1);
        }
      },
    });
  },

  async handleLineDelete(params) {
    if (params instanceof Object) {
      var transfer = params;
    } else if (typeof params == 'string') {
      var transfer = {url: params};
    }

    // if (!params) return Modal.info({title: '信息', content: '请选择一条订单进行结束'});
    let res = await axios.ajax({
      url: transfer.url,
      params: transfer.data
    });
    if (res && res.code == 0) {
      this.setState({
        orderInfo: res.content,
        defaultModalVisble: true,
      });
    }
  },

  async openOrderMapPage(params) {
    if (params instanceof Object) {
      var transfer = params;
    } else if (typeof params == 'string') {
      var transfer = {url: params};
    }
    // if (!params) {Modal.info({title: '信息', content: '请先选择一条订单'})}
    return window.open(transfer.url, '_blank');
  },
};

